--#include "data\config\boss\TempleWoMaConfig.lua" once

TempleWoMaStatesColumn = 
{   
    {"hasEnterFlag"},               --玩家是否进入活动
    
}

TempleWoMaStates = Lang.Rank.TempleWoMaStates       --神殿沃玛玩家



local TempleWoMaTimer = 
{
    SelectTime = 30,
}
local BossType = {}
BossType.smallBoss = 1
BossType.bigBoss = 2

function ResisterTempleWoMaProc()
    --  该副本是需要掉线恢复的
    SceneExitDispatcher.register( TempleWoMaConfig.sceneId, OnCommonSceneExit )                  --注册离开场景、副本后触发的事件
end

function InitTempleWoMaStates()
    --print("*****>>InitTempleWoMaStates...")
    Rank.Init(TempleWoMaStates, TempleWoMaStates, TempleWoMaStatesColumn, 1000, 0)
end

function GetTempleWoMaStates( actorId )
    return GetCommonRankColumnValueById(actorId, TempleWoMaStates, 0) or 0
end

function SetTempleWoMaStates(actorId, stateval )
    return SetCommonRankColumnValueById(actorId, TempleWoMaStates, 0, 0, stateval)
end

--[[
设置boss刷新定时器
bosstype: 1:set smallBossTimer  2: set bigBossTimer 
]]
function SetTempleWoMaKillBossTime( bosstype )
    --print("SetTempleWoMaKillBossTime.. ")
    local sVar = System.getStaticVar()
    --设置击杀时间
    if not sVar.smallBossTimer then
        sVar.smallBossTimer = 0
    end
    if not sVar.bigBossTimer then
        sVar.bigBossTimer = 0
    end
    
    local now = System.getCurrMiniTime()
    if bosstype == BossType.smallBoss then
        sVar.smallBossTimer = now
    elseif bosstype == BossType.bigBoss then
        sVar.bigBossTimer = now
    end   
    SetTempleWoMaLastKillBoss( bosstype )
    SetTempleWoMaBossKillFlag( bosstype , 1)

    if GetTempleWoMaBossCountInSence() == 0 then
        --击杀所有小Boss后，大Boss直接刷新
        if  sVar.smallBossKillFlag == 1 and sVar.bigBossKillFlag == 0 then
            TempleWoMaFreshBossLogic()
        --大小BOSS都被击杀，计时开始下一轮
        elseif sVar.smallBossKillFlag == 1 and sVar.bigBossKillFlag == 1 then
            --print("SetTempleWoMaKillBossTime regScriptTimer TempleWoMaFreshBossLogic")
            local NpcPtr = System.getGlobalNpc()
            Actor.unregScriptCallback(NpcPtr, "TempleWoMaFreshBossLogic")
            Actor.regScriptTimer(NpcPtr, 0, TempleWoMaConfig.timer.monsterFreshCd * 1000,  0, 1, "TempleWoMaFreshBossLogic")
        end
    end
end

--[[
是否可以刷新BOSS
bosstype: 1:smallBoss  2:  bigBoss
return：true or  false
]]
function IsAllowFreshTempleWoMaBoss( bosstype )
    --print("IsAllowFreshTempleWoMaBoss.. ")
    local sVar = System.getStaticVar()
    --设置击杀标记
    if not sVar.smallBossKillFlag then
        sVar.smallBossKillFlag = 0
    end
    if not sVar.bigBossKillFlag then
        sVar.bigBossKillFlag = 0
    end

    if GetTempleWoMaLastKillBoss( ) == bosstype then
        --print("IsAllowFreshTempleWoMaBoss bosstype is same with lastKillBoss")
        return false
    end

    if GetTempleWoMaBossCountInSence() == 0 then
        --注：刷新小boss时需要检查大boss是否已被击杀，
        ------刷新大boss时需要检查小boss是否已被击杀，
        if bosstype == BossType.smallBoss then
            if sVar.bigBossKillFlag == 1 and IsArriveTempleWoMaBossFreshTime(BossType.smallBoss) then
                --print("IsAllowFreshTempleWoMaBoss allow smallBoss")
                return true
            end
        elseif bosstype == BossType.bigBoss then
            if sVar.smallBossKillFlag == 1 and IsArriveTempleWoMaBossFreshTime(BossType.bigBoss) then
                --print("IsAllowFreshTempleWoMaBoss allow bigBoss")
                return true
            end
        end
    end
    return false
end

--[[
是否到达Boss刷新时间
bosstype: 1:smallBoss  2:  bigBoss
return：true or  false
]]
function IsArriveTempleWoMaBossFreshTime( bosstype )
    --print("IsArriveTempleWoMaBossFreshTime ..")
    local sVar = System.getStaticVar()
    --设置击杀时间
    if not sVar.smallBossTimer then
        sVar.smallBossTimer = 0
    end
    if not sVar.bigBossTimer then
        sVar.bigBossTimer = 0
    end
    local now   = System.getCurrMiniTime()
    --print("IsArriveTempleWoMaBossFreshTime  smallBossTimer= "..sVar.smallBossTimer.." bigBossTimer= "..sVar.bigBossTimer.." now= "..now)
    local minus = 0
    if bosstype == BossType.smallBoss then
        minus = now - sVar.smallBossTimer
        if  minus >= TempleWoMaConfig.timer.monsterFreshCd then
            --print("IsArriveTempleWoMaBossFreshTime arrive smallBoss fresh, minus= "..minus)
            return true
        end
    elseif bosstype == BossType.bigBoss then
        minus = now - sVar.bigBossTimer
        if  minus >= TempleWoMaConfig.timer.monsterFreshCd then
            --print("IsArriveTempleWoMaBossFreshTime arrive bigBoss fresh,minus= "..minus)
            return true
        end
    end
    return false
end


--[[
设置boss击杀标记
bosstype: 1:set smallBossKillFlag  2: set bigBossKillFlag 
flagval: 1 :击杀 or 0：未击杀
]]
function SetTempleWoMaBossKillFlag( bosstype , flagval)
    --print("SetTempleWoMaBossKillFlag ..bosstype= "..bosstype.." val= "..tostring(flagval))
    local sVar = System.getStaticVar()

    if not sVar.smallBossKillFlag then
        sVar.smallBossKillFlag = 0
    end
    if not sVar.bigBossKillFlag then
        sVar.bigBossKillFlag = 0
    end
    --设置击杀标记
    if bosstype == BossType.smallBoss then
        sVar.smallBossKillFlag = flagval
    elseif bosstype == BossType.bigBoss then
        sVar.bigBossKillFlag = flagval
    end
    --print("SetTempleWoMaBossKillFlag ..  smallkillFlag= "..tostring(sVar.smallBossKillFlag).." bigkillFlag= "..tostring(sVar.bigBossKillFlag))
end

function SetTempleWoMaLastKillBoss( bosstype )
    --print("SetTempleWoMaLastKillBoss.. ")
    local sVar = System.getStaticVar()
    if not sVar.lastKillBoss then
        sVar.lastKillBoss = BossType.smallBoss
    end
    sVar.lastKillBoss = bosstype
end

function GetTempleWoMaLastKillBoss( )
    --print("GetTempleWoMaLastKillBoss..")
    local sVar = System.getStaticVar()
    if not sVar.lastKillBoss then
        sVar.lastKillBoss = BossType.smallBoss
    end
    return sVar.lastKillBoss
end

--[[
TempleWoMa 刷新boss方法
]]
function TempleWoMaFreshBossLogic(  )
    --print("TempleWoMaFreshBossLogic.. ")
    --判断是否能刷新小boss
    if GetTempleWoMaSmallBossCountInSence() == 0 and IsAllowFreshTempleWoMaBoss(BossType.smallBoss) then
        TempleWoMaFreshBoss(BossType.smallBoss)
        return
    end
    --判断是否能刷新大boss
    if GetTempleWoMaBigBossCountInSence() == 0  and IsAllowFreshTempleWoMaBoss(BossType.bigBoss) then
        TempleWoMaFreshBoss(BossType.bigBoss)
        return
    end
    return
end

--[[
活动开始（@DoBoss 34 0）
]]
function OnTempleWoMaBegin( )
    --print("OnTempleWoMaBegin... ")
    ClearCommonRank( TempleWoMaStates )       --清除玩家进入信息
    local dVar = System.getDyanmicVar()
    dVar.TempleWoMaBeginTime  = System.getCurrMiniTime()            --活动开启时间
    --print("TempleWoMaBeginTime= "..dVar.TempleWoMaBeginTime)
     
    local NpcPtr = System.getGlobalNpc()
    Actor.unregScriptCallback(NpcPtr, "OnTempleWoMaStop")
    Actor.regScriptTimer(NpcPtr, 0, TempleWoMaConfig.timer.activityTime*1000, TempleWoMaConfig.timer.activityTime*1000, 1, 
        "OnTempleWoMaStop")

    TempleWoMaPlayerOut()
    System.broadcastTipmsg( Lang.ScriptTips.TempleWoMa001, ttScreenCenter + ttChatWindow )  
    TempleWoMaFreshBoss(BossType.smallBoss)
    
    Actor.unregScriptCallback(NpcPtr, "TempleWoMaFreshBossLogic")
end


--踢出活动玩家
function TempleWoMaPlayerOut()
    local playerList = LuaHelp.getSceneActorListById( TempleWoMaConfig.sceneId ) or {}
    for i, player in ipairs( playerList or {} ) do
        Actor.returnCity( player )
    end
end

function ClearTempleWoMaAllMonsters()

    for i,BossCfg in ipairs(TempleWoMaConfig.smallBossS or {}) do
        clearScenceMonster( BossCfg )
    end
    for i,BossCfg in ipairs(TempleWoMaConfig.bigBossS or {}) do
        clearScenceMonster( BossCfg )
    end
end

--[[
活动结束
]]
function OnTempleWoMaStop( )
    --print("OnTempleWoMaStop... ")
    local dVar = System.getDyanmicVar()
    dVar.TempleWoMaBeginTime  = nil
    local NpcPtr = System.getGlobalNpc()    
    Actor.unregScriptCallback(NpcPtr, "OnTempleWoMaStop")

    TempleWoMaPlayerOut()
    ClearTempleWoMaAllMonsters()
    System.broadcastTipmsg( Lang.ScriptTips.TempleWoMa002, ttScreenCenter + ttChatWindow )  

    --注销定时器刷怪方法
    Actor.unregScriptCallback(NpcPtr, "TempleWoMaFreshBossLogic")
end


--[[
进入神殿沃玛副本
]]
function EnterTempleWoMa( sysarg )
    --print("EnterTempleWoMa ... ")

    if not IsTempleWoMaValidTime() then         --不在活动时间内
        Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0038, ttFlyTip)
        return
    end
    local actorId  = Actor.getActorId( sysarg )
    --[[if  GetTempleWoMaStates( actorId ) > 0 then
        Actor.sendTipmsg(sysarg, Lang.ScriptTips.TempleWoMa003, ttFlyTip)
        return
    end ]] 
   
    --进入场景
    if CommonEnterScence( sysarg, TempleWoMaConfig, 0, " ", nil, nil ) then  
        --SetTempleWoMaStates( actorId, 1 )

        InitTempleWoMaRightPanel( sysarg )
        UpdateTempleWoMaPanelInfo(sysarg,false)
    end
end

--[[
清除玩家进入活动标记（在跨天时调用 @DoBoss 34 1）
]]
function CleanTempleWoMaEnterFlag( sysarg )
    --local actorId  = Actor.getActorId( sysarg )
    --SetTempleWoMaStates( actorId, 0 )
end


--[[
刷新大BOSS和精英怪
--bossType: 1:小BOSS； 2：大BOSS
]]
function TempleWoMaFreshBoss(bossType)
    --print("TempleWoMaFreshBoss...  bossType= "..bossType)
    local bossCfg = (bossType == BossType.smallBoss) and TempleWoMaConfig.smallBossS or TempleWoMaConfig.bigBossS
    FreshCommonMonsters( bossCfg , 0, false )
    UpdateTempleWoMaPanelInfo(sysarg,true)
end

--[[只获取BOSS数量]]
function GetTempleWoMaBossCountInSence()
    local counts = 0
    for i,Boss in ipairs(TempleWoMaConfig.smallBossS) do
        local num = System.getMonsterCountById( Boss.monsterId, Boss.sceneId, 0 )
        counts = counts + num
    end
    for i,Boss in ipairs(TempleWoMaConfig.bigBossS) do
        local num = System.getMonsterCountById( Boss.monsterId, Boss.sceneId, 0 )
        counts = counts + num
    end
    return counts
end
--[[只获取小BOSS数量]]
function GetTempleWoMaSmallBossCountInSence()
    local counts = 0
    for i,Boss in ipairs(TempleWoMaConfig.smallBossS) do
        local num = System.getMonsterCountById( Boss.monsterId, Boss.sceneId, 0 )
        counts = counts + num
    end
    return counts
end
--[[只获取大BOSS数量]]
function GetTempleWoMaBigBossCountInSence()
    local counts = 0
    for i,Boss in ipairs(TempleWoMaConfig.bigBossS) do
        local num = System.getMonsterCountById( Boss.monsterId, Boss.sceneId, 0 )
        counts = counts + num
    end
    return counts
end

--[[
广播boss事件信息
type： 1：mob  2：die
]]
function TempleWoMaBroadcastBossEvent( type, monId, lastHitKiller  )
    for monsterIdx,monsterId in ipairs(TempleWoMaConfig.broadcastBoss) do
        if monId == monsterId then
            local bossName = System.getMonsterNameById(monId)           --BOSS名字，去掉数字
            if not bossName then
                return
            end
            if type == 1 then
                System.broadcastTipmsg( string.format( Lang.ScriptTips.TempleWoMaBossMobMsg01, bossName), ttScreenCenter + ttChatWindow)
            else
                if not lastHitKiller then
                    return
                end
                System.broadcastTipmsg( string.format( Lang.ScriptTips.TempleWoMaBossKillMsg01, bossName, Actor.getName( lastHitKiller)), ttScreenCenter + ttChatWindow )
            end
        end
    end
end

--[[
小Boss mob时触发事件 
]]
function OnTempleWoMaSmallBosssMob(monster, monId, sceneId, x, y)
   TempleWoMaBroadcastBossEvent( 1, monId, nil)
   SetTempleWoMaBossKillFlag( BossType.smallBoss, 0 )
   --目前一轮结束刷怪（小boss+大boos击杀后为一轮）将大boss击杀标记清空
   SetTempleWoMaBossKillFlag( BossType.bigBoss, 0 )
end

--[[
击杀小的Boss[击杀三个小BOSS，刷新出大BOSS]
]]
function OnTempleWoMaSmallBosssDie( monster, sysarg, monId, lastHitKiller )
    --print("OnTempleWoMaSmallBosssDie monId:"..monId)
    TempleWoMaBroadcastBossEvent( 2, monId, lastHitKiller)
    SetTempleWoMaKillBossTime(BossType.smallBoss)
    --所有小BOSS都被击杀，刷新大BOSS
    --[[if GetTempleWoMaBossCountInSence() == 0 and GetTempleWoMaSmallBossCountInSence() == 0 then
        --print("fresh BossBossS...")
        TempleWoMaFreshBoss(BossType.bigBoss)
    end
    --全场怪为0时重新刷新大BOSS
    if GetTempleWoMaBossCountInSence() == 0 then
        TempleWoMaFreshBoss(BossType.bigBoss)
    end]]
    UpdateTempleWoMaPanelInfo(sysarg,true)
end


--[[
大Boss mob时触发事件 
]]
function OnTempleWoMaBigBosssMob(monster, monId, sceneId, x, y)
    TempleWoMaBroadcastBossEvent( 1, monId, nil)
    SetTempleWoMaBossKillFlag( BossType.bigBoss, 0 )
end

--[[
击杀大的Boss[击杀大BOSS和两个精英BOSS，刷新小BOSS]
]]
function OnTempleWoMaBigBosssDie( monster, sysarg, monId, lastHitKiller )
    --print("OnTempleWoMaBigBosssDie monId:"..monId)
    TempleWoMaBroadcastBossEvent( 2, monId, lastHitKiller)
    SetTempleWoMaKillBossTime(BossType.bigBoss)
    --所有的大BOSS都被击杀，则重新新小BOSS
    --[[if GetTempleWoMaBossCountInSence() == 0 and GetTempleWoMaBigBossCountInSence() == 0 then
        --print("fresh bigBossS...")
        TempleWoMaFreshBoss(BossType.smallBoss)
    end
    --全场怪为0时重新刷新小BOSS
    if GetTempleWoMaBossCountInSence() == 0 then
        TempleWoMaFreshBoss(BossType.smallBoss)
    end]]
    UpdateTempleWoMaPanelInfo(sysarg,true)
end

--[[
是否在场景内
]]
function IsInTempleWoMa( sceneId )
    --print("IsInTempleWoMa, sceneId="..sceneId)
    if sceneId == TempleWoMaConfig.sceneId then
        return true
    end
    return false
end

--[[
是否在活动中
]]
function IsTempleWoMaValidTime( )
    local now   = System.getCurrMiniTime()
    local dVar  = System.getDyanmicVar()
    if dVar.TempleWoMaBeginTime and dVar.TempleWoMaBeginTime > 0 and now-dVar.TempleWoMaBeginTime >=0 and
        now-dVar.TempleWoMaBeginTime < TempleWoMaConfig.timer.activityTime then
        return true
    end
    return false
end

function GetTempleWoMaLastTime( )
    local dVar = System.getDyanmicVar()
    if not dVar.TempleWoMaBeginTime then        --活动未开始
        return 0
    end

    local now = System.getCurrMiniTime()    
    local lastTime = dVar.TempleWoMaBeginTime + TempleWoMaConfig.timer.activityTime - now
    if lastTime > 0 then
        return lastTime
    end
    return 0
end


--[[
初始化右侧面板
]]
function InitTempleWoMaRightPanel( sysarg )
    local sceneId = Actor.getSceneId( sysarg )
    if not IsInTempleWoMa( sceneId ) then           --不在星宫内
        return
    end

    local activityType      = 2             --活动
    TempleWoMaConfig.restTime = GetTempleWoMaLastTime()
    
    local panelParams       = {}
    panelParams.sts             = 1                 --进行中
    panelParams.contents        = {}
    
    FillTempleWoMaPanelContents(panelParams)
    panelParams.buttons = {1}   --1：退出副本，2-单倍领奖，3-双倍领奖，4-查看排行榜，5-押镖寻路, 6-打开答题面板，7-放弃押镖，8-寻找活动NPC

    OpenCommonStylePanel( sysarg, activityType, enRightPanelActivity_TempleWoMa, nil, Lang.ScriptTips.TempleWoMaName, TempleWoMaConfig, 8, buttons, panelParams )
end



--[[填充面板内容信息]]
function FillTempleWoMaPanelContents( panelParams )
    if not panelParams.contents then
        panelParams.contents = {}
    end

    --小BOSS状态刷新 [已刷出-->已击杀-->未刷出-->已刷出]
    table.insert( panelParams.contents,  Lang.ScriptTips.panelCont46)
    local stat = ""
    local monsterName  = ""
    local num = 0
    for i,Boss in ipairs(TempleWoMaConfig.smallBossS) do
        monsterName = System.getMonsterNameById(Boss.monsterId)
        num = System.getMonsterCountById( Boss.monsterId, Boss.sceneId, 0 )
        if num <= 0 then
            if GetTempleWoMaSmallBossCountInSence() > 0 then
                stat = Lang.ScriptTips.panelCont44   --已击杀
            else
                stat = Lang.ScriptTips.panelCont45   --未刷出
            end
        else
            stat = Lang.ScriptTips.panelCont43   --已刷出
        end
        local info = string.format( Lang.ScriptTips.panelCont42 , monsterName, stat )   --[XXXboss:XXXstat]
        info = string.format(Lang.ScriptTips.panelCont48, info)
        table.insert( panelParams.contents, info )
    end

    --只显示超级大BOSS状态信息,精英怪的不显示
    local Boss = TempleWoMaConfig.bigBossS[1]      --第一个配置的是大BOSS
    monsterName = System.getMonsterNameById(Boss.monsterId)
    num = System.getMonsterCountById( Boss.monsterId, Boss.sceneId, 0 )
    if num <= 0 then
        if  GetTempleWoMaBigBossCountInSence() > 0 then
            stat = Lang.ScriptTips.panelCont44   --已击杀
        else
            stat = Lang.ScriptTips.panelCont45   --未刷出
        end
    else
        stat = Lang.ScriptTips.panelCont43   --已刷出
    end
    local info = string.format( Lang.ScriptTips.panelCont42, monsterName, stat )   --[XXXboss:XXXstat]
    info = string.format(Lang.ScriptTips.panelCont48, info)
    table.insert( panelParams.contents, info )

end

--[[刷新BOSS状态]]
--isBroadcast:是否对场景内玩家进行广播
function UpdateTempleWoMaPanelInfo(sysarg, isBroadcast)
    --print("UpdateTempleWoMaPanelInfo ... ")
    --获取所有大小BOSS状态信息，更新在左侧的面板信息中
        
    local panelParams       = {}
    panelParams.contents    = {}
    panelParams.restTime    = GetTempleWoMaLastTime()

    FillTempleWoMaPanelContents( panelParams)

    local buttons1      = {1}
    local buttons2      = {1}

    --广播给场景内所有的玩家
    if not isBroadcast then
        ChangeCommonStylePanel( sysarg, 1, 8, TempleWoMaConfig, buttons1, buttons2, panelParams )
    else
        local playerList = LuaHelp.getSceneActorListById( TempleWoMaConfig.sceneId ) or {}
        for i,player in ipairs( playerList ) do
            ChangeCommonStylePanel( player, 1, 8, TempleWoMaConfig, buttons1, buttons2, panelParams )
        end
    end
end

InitTempleWoMaStates()
ResisterTempleWoMaProc()

